package com.cskaoyan.wordmemorize.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.ExcelParseException;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocChapBookDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapBookMapper;
import com.cskaoyan.wordmemorize.dto.admin.VocExcelDTO;

import java.util.Map;

public class VocSingleExcelValidListener extends AnalysisEventListener<VocExcelDTO> {

    VocBookMapper vocBookMapper;
    VocChapBookMapper vocChapBookMapper;

    public VocSingleExcelValidListener(VocBookMapper vocBookMapper, VocChapBookMapper vocChapBookMapper) {
        this.vocBookMapper = vocBookMapper;
        this.vocChapBookMapper = vocChapBookMapper;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        if (headMap.size() != 8 || !headMap.get(0).equals("单词") || !headMap.get(1).equals("英式音标") || !headMap.get(2).equals("美式音标") || !headMap.get(3).equals("英式发音文件名") || !headMap.get(4).equals("美式发音文件名") || !headMap.get(5).equals("释义") || !headMap.get(6).equals("例句") || !headMap.get(7).equals("单词位置"))
            throw new ExcelParseException(
                    ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                    ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage()+1);

    }

    @Override
    public void invoke(VocExcelDTO vocExcelDTO, AnalysisContext analysisContext) {
        // 校验表内数据是否为空
        if (vocExcelDTO.getContent() == null || vocExcelDTO.getContent().isEmpty() || vocExcelDTO.getUkPhonetic() == null || vocExcelDTO.getUkPhonetic().isEmpty() || vocExcelDTO.getUsPhonetic() == null || vocExcelDTO.getUsPhonetic().isEmpty() || vocExcelDTO.getUkPronounciation() == null || vocExcelDTO.getUkPronounciation().isEmpty() || vocExcelDTO.getUsPronounciation() == null || vocExcelDTO.getUsPronounciation().isEmpty() || vocExcelDTO.getInterpretations() == null || vocExcelDTO.getInterpretations().isEmpty() || vocExcelDTO.getExampleSentences() == null || vocExcelDTO.getExampleSentences().isEmpty() || vocExcelDTO.getBookInfos() == null || vocExcelDTO.getBookInfos().isEmpty())
            throw new ExcelParseException(
                    ResultCodeEnum.PARAM_ERROR,
                    ResultCodeEnum.PARAM_ERROR.getMessage()+2);

        // 校验是否为词性.释义内容
        String[] split = vocExcelDTO.getInterpretations().split("/n");
        for (String s : split) {
            if (s.split("\\.").length != 2)
                throw new ExcelParseException(
                        ResultCodeEnum.PARAM_ERROR,
                        ResultCodeEnum.PARAM_ERROR.getMessage()+3);
        }


        // 校验是否为例句和翻译交替出现，以换行符分隔
        if (vocExcelDTO.getExampleSentences().split("\n").length % 2 != 0)
            throw new ExcelParseException(
                    ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                    ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage()+4);

        // 校验单词位置是否为 书籍#章节 或者 书籍，并且校验书籍和对应章节是否存在

        if (vocExcelDTO.getBookInfos().split("#").length == 1 || vocExcelDTO.getBookInfos().split("#").length == 2) {
            String[] bookInfoArray = vocExcelDTO.getBookInfos().split("#");
            if (bookInfoArray.length == 0) {
                throw new ExcelParseException(
                        ResultCodeEnum.PARAM_ERROR,
                        ResultCodeEnum.PARAM_ERROR.getMessage()+5);
            }
            LambdaQueryWrapper<VocBookDO> vocBookDOQueryWrapper = new LambdaQueryWrapper<>();
            vocBookDOQueryWrapper
                    .eq(VocBookDO::getName, bookInfoArray[0]);
            VocBookDO vocBookDO = vocBookMapper.selectOne(vocBookDOQueryWrapper);
            if (vocBookDO == null) {
                throw new ExcelParseException(
                        ResultCodeEnum.VOC_EXCEL_PARSE_ERROR,
                        ResultCodeEnum.VOC_EXCEL_PARSE_ERROR.getMessage()+6);

            } else {
                if (bookInfoArray.length == 2) {
                    LambdaQueryWrapper<VocChapBookDO> eq = new LambdaQueryWrapper<VocChapBookDO>()
                            .eq(VocChapBookDO::getChapterName, bookInfoArray[0]);
                    VocChapBookDO vocChapBookDO = vocChapBookMapper.selectOne(eq);
                    if (vocChapBookDO == null)
                        throw new ExcelParseException(
                                ResultCodeEnum.VOC_EXCEL_PARSE_ERROR,
                                ResultCodeEnum.VOC_EXCEL_PARSE_ERROR.getMessage()+7);
                }
            }

        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

}
